<template>
  <common-page title="重置密码">
    <el-steps :active="stepActive" simple finish-status="success">
      <el-step title="填写新密码" />
      <el-step title="完成重置" />
    </el-steps>
    <el-form v-if="stepActive == 0" ref="formRef" :model="form" :rules="rules" :label-width="120">
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号码" size="large">
          <template #prepend>+86</template>
        </el-input>
      </el-form-item>
      <el-form-item label="短信验证码" prop="code">
        <div class="yzm">
          <el-input v-model="form.code" placeholder="请输入6位短信验证码" size="large">
            <template #append>
              <el-button @click="sendCode" :disabled="disabled" size="large">
                获取验证码
                <span v-if="disabled">({{ time }})</span>
              </el-button>
            </template>
          </el-input>
        </div>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input
          v-model="form.password"
          show-password
          placeholder="请输入包含英文、数字的8位以上新密码"
          size="large"
        ></el-input>
        <dx-password-strength v-model="form.password"></dx-password-strength>
      </el-form-item>
      <el-form-item label="确认新密码" prop="password_confirm">
        <el-input
          v-model="form.password_confirm"
          show-password
          placeholder="请再一次输入新密码"
          size="large"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%; margin: 10px 0" @click="submit" size="large">提交</el-button>
      </el-form-item>
    </el-form>
    <el-result
      v-if="stepActive == 1"
      icon="success"
      title="密码重置成功"
      sub-title="请牢记自己的新密码,返回登录后使用新密码登录"
    >
      <template #extra>
        <el-button type="primary" @click="backLogin">返回登录</el-button>
      </template>
    </el-result>
  </common-page>
</template>

<script setup lang="ts">
import to from '@/utils/to'
import { IAuth } from '@/api/auth/type'
import CommonPage from './components/CommonPage.vue'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const stepActive = ref<number>(0)
const formRef = ref<FormInstance>()

// 表单参数
const form: IAuth.resetPasswordForm = reactive({
  mobile: '',
  code: '',
  password: '',
  password_confirm: ''
})
// 表单验证
const rules = reactive<FormRules<IAuth.resetPasswordForm>>({
  mobile: [
    { required: true, len: 11, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
  ],
  code: [{ required: true, len: 6, message: '请输入6位短信验证码', trigger: 'blur' }],
  password: [{ required: true, min: 8, max: 20, message: '密码长度为8-20位', trigger: 'blur' }],
  password_confirm: [
    { required: true, message: '请再次输入新的密码' },
    {
      validator: (_, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }
    }
  ]
})

// 发送验证码
const disabled = ref<boolean>(false)
const time = ref<number>(0)
const sendCode = () => {
  disabled.value = true
  time.value = 60
  var t = setInterval(() => {
    time.value -= 1
    if (time.value < 1) {
      clearInterval(t)
      disabled.value = false
      time.value = 0
    }
  }, 1000)
}

const backLogin = () => router.push({ path: '/login' })

// 提交
const submit = async () => {
  try {
    if (await to(formRef.value!.validate())) return
  } catch (error) {}
}
</script>

<style scoped></style>
